const {db} = require('../config');
const moment = require('moment');
const { Sequelize,DataTypes,Op } = require('sequelize');
const User =  require('./model/user');
const Merchant =  require('./model/merchant');
const Device =  require('./model/device');
const AlarmContactGroup =  require('./model/alarm_contact_group.js');
const AlarmContactGroupDevice =  require('./model/alarm_contact_group_device.js');
const AlarmContactGroupUser =  require('./model/alarm_contact_group_user.js');
const AlarmEvent =  require('./model/alarm_event.js');
const AlarmNoticeRecord =  require('./model/alarm_notice_record.js');
const Face =  require('./model/face.js');
const ReportNoticeRecord =  require('./model/report_notice_record.js');
const ReportUser =  require('./model/report_user.js');
const ReportUserGroup =  require('./model/report_user_group.js');
const ReportUserGroupDevice =  require('./model/report_user_group_device.js');
const ReportUserGroupReportUser =  require('./model/report_user_group_report_user.js');
const Config =  require('./model/config.js');


// 方法 2: 分别传递参数 (其它数据库)
const sequelize = new Sequelize(db.dbname, db.username, db.password, {
    host: db.host,
    port:db.port,
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        idle: 30000
    },
    // dialectOptions: {
    //     dateStrings: true,
    //     typeCast: true
    // },
    // logging:false,  // 关闭日志显示
    timezone:'+08:00' // 时间加8个小时
    //最好关掉timestamps , 框架自动帮你添加时间到UpdatedAt上边
});
sequelize.sync();
async function testConnect(){
    try {
        await sequelize.authenticate();
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
}

/*
// 标准同步
// 只有当数据库中不存在与模型同名的数据表时，才会同步
sequelize.sync()
// 动态同步
// 修改同名数据表结构，以适用模型。
sequelize.sync({alter: true})
// 强制同步
// 删除同名数据表后同步，谨慎使用，会导致数据丢失
sequelize.sync({force: true})

// 另外，当你指定表与表之间的关联后，修改被关联的表结构时会抛出异常。
// 需要先注释掉关联代码，然后更新同步模型后，再取消掉注释即可。

// 再另外，当你有新的关联时必须使用动态同步才会生效。

*/

let DBModel = {
    // testConnect:testConnect,
    User:User(sequelize,DataTypes),
    Merchant:Merchant(sequelize,DataTypes),
    Device:Device(sequelize,DataTypes),
    AlarmContactGroup:AlarmContactGroup(sequelize,DataTypes),
    AlarmContactGroupDevice:AlarmContactGroupDevice(sequelize,DataTypes),
    AlarmContactGroupUser:AlarmContactGroupUser(sequelize,DataTypes),
    AlarmEvent:AlarmEvent(sequelize,DataTypes),
    AlarmNoticeRecord:AlarmNoticeRecord(sequelize,DataTypes),
    Face:Face(sequelize,DataTypes),
    ReportNoticeRecord:ReportNoticeRecord(sequelize,DataTypes),
    ReportUser:ReportUser(sequelize,DataTypes),
    ReportUserGroup:ReportUserGroup(sequelize,DataTypes),
    ReportUserGroupDevice:ReportUserGroupDevice(sequelize,DataTypes),
    ReportUserGroupReportUser:ReportUserGroupReportUser(sequelize,DataTypes),
    Config:Config(sequelize,DataTypes)
};
// 快速同步数据表，扩展新字段
for(let modelName in DBModel){
    DBModel[modelName].sync({alter: true});
}

// 创建关系

DBModel.AlarmEvent.belongsTo(DBModel.Device, { foreignKey: 'device_id', targetKey: 'id', as: 'device' });

DBModel.AlarmEvent.belongsTo(DBModel.Face, { foreignKey: 'face_id', targetKey: 'id', as: 'face' });


module.exports = DBModel;
